﻿using FieldTools.Client.Entity.Model;
using FieldTools.Util;
using FieldTools.Util.Helper;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Linq;

namespace FieldTools.Client.Entity.MyQQ
{
    /// <summary>
    /// 预定义指令说明集合
    /// </summary>
    public class PredefineContents
    {
        static IdHelper idHelper = new();
        /// <summary>
        /// 预定义指令说明集合数据
        /// </summary>
        public ObservableCollection<PredefineContent> PredefineContentItems { get; set; } = new();
        /// <summary>
        /// 空初始化
        /// </summary>
        public PredefineContents()
        {
            PredefineContentItems.Add(new PredefineContent() {
                Id = idHelper.NextId().ToString(),
                PredefineField = "fulitu1",
                ScriptContent = @"using FieldTools.Client.Business.Util;
public string Excute(){
 var url = UtilHttpApi.GetBuyersShow().Result;
 return $""[CQ:image,file={url}]"";
}",
                Remark = "淘宝接口获取的一个福利图",
                IsOn = true
            });
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="predefineContents">传入集合</param>
        public PredefineContents(List<PredefineContent> predefineContents)
        {
            if (predefineContents!=null && predefineContents.Count != 0)
            {
                PredefineContentItems = [.. predefineContents];
            }
            else
            {
                Console.WriteLine("表格数据构建失败，请检查配置是否有误");
            }
        }
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="predefineContents">传入字典数据</param>
        //public PredefineContents(Dictionary<string, string> predefineContents)
        //{
        //    try
        //    {
        //        if (predefineContents!=null && predefineContents.Count != 0)
        //        {
        //            var listContent = new List<PredefineContent>();
        //            predefineContents.ForEach(content =>
        //            {
        //                listContent.Add(new()
        //                {
        //                    Id = idHelper.NextId().ToString(),
        //                    PredefineField = content.Key,
        //                    Content = content.Value
        //                });
        //            });
        //            PredefineContentItems = new(listContent);
        //        }
        //    }
        //    catch (Exception ex)
        //    {

        //        Console.WriteLine("表格数据构建失败，请检查传入字典是否有误," + ex.Message);
        //    }
        //}
        /// <summary>
        /// 传入json字符串
        /// </summary>
        /// <param name="predefineContentsStr"></param>
        public PredefineContents(string predefineContentsStr)
        {
            try
            {
                if (!predefineContentsStr.IsNullOrEmpty())
                {
                    var predefineContents = predefineContentsStr.ToObject<List<PredefineContent>>();
                    PredefineContentItems = [.. predefineContents];
                }
                else
                {
                    Console.WriteLine("表格数据构建失败，请检查配置是否有误");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("表格数据构建失败，请检查json字符串是否有误," + ex.Message);
            }

        }

        /// <summary>
        /// 通过field字段获取content
        /// </summary>
        /// <param name="predefineField"></param>
        /// <returns></returns>
        public PredefineContent? GetContentByField(string predefineField) => PredefineContentItems.Count > 0 ? PredefineContentItems.First(item=>!string.IsNullOrEmpty(item.PredefineField) && item.PredefineField.Equals(predefineField)) : null;
    }
    /// <summary>
    /// 预定指令字段
    /// </summary>
    public class PredefineContent : CustomScript
    {
        /// <summary>
        /// 预定义字段
        /// </summary>
        public string? PredefineField { get; set; }
        /// <summary>
        /// 字段说明
        /// </summary>
        public string? Remark { get; set; }
        /// <summary>
        /// 是否启用
        /// </summary>
        public bool IsOn { get; set; }
    }
}
